
var menuForm;
var formType;

$(function () {
    //初始化表格
    createBootstrapTreeTable();

    //初始化ztree树
    createZtree();

    //初始化单选框样式
    $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
        checkboxClass: 'icheckbox_flat-green',
        radioClass: 'iradio_flat-green'
    });

});

//初始化表格
function createBootstrapTreeTable(){
    var toolbar1 =
        '<div id="toolbar" class="btn-group">' +
        '<button id="btn_add" type="button" class="btn btn-info btn-xs" onclick="add()">' +
        '<i class="fa fa-plus"></i> 新增 ' +
        '</button>' +
        '</div>';
    var columns = [
        {
            field: 'name',
            title: '菜单名称',
            align:'center',
            valign:'middle',
            formatter: function (value, row, index) {
                if (row.icon == null || row.icon == '') {
                    return value
                } else {
                    return '<i class="' + row.icon + '"><span>' + value + '</span></i>'
                }
            }
        },
        {
            field: 'menuType',
            title: '菜单类型',
            align:'center',
            valign:'middle',
            formatter: function (value, row, index) {
                if (row.menuType  == '0') {
                    return '<button id="btn_edit" type="button" class="btn bg-navy disabled  btn-xs" >目录</button>'
                } else  if (row.menuType  == '1'){
                    return '<button id="btn_edit" type="button" class="btn bg-purple disabled btn-xs" >菜单</button>'
                }else  if (row.menuType  == '2'){
                    return '<button id="btn_edit" type="button" class="btn bg-orange disabled  btn-xs" >按钮</button>'
                }
            }
        },
        {
            field: 'permission',
            title: '权限标识',
            align:'center',
            valign:'middle',
        },
        {
            field: 'href',
            title: '请求地址',
            align:'center',
            valign:'middle',
        },
        {
            field: 'sort',
            title: '菜单排序',
            align:'center',
            valign:'middle',
        },
        {
            field: 'isShows',
            title: '菜单状态',
            align:'center',
            valign:'middle',
            formatter: function (value, row, index) {
                if (row.isShows == null || row.isShows  == '1') {
                    return '<button id="btn_edit" type="button" class="btn btn-warning disabled  btn-xs" >隐藏</button>'
                } else {
                    return '<button id="btn_edit" type="button" class="btn btn-success disabled  btn-xs" >显示</button>'
                }
            }
        },
        {
            field: 'id',
            title: '操作',
            align:'center',
            valign:'middle',
            formatter: function (value, row, index) {
                return ' <button id="btn_edit" type="button" class="btn btn-primary  btn-xs" onclick="update(\'' + value + '\')"><i class="fa fa-edit"></i> 修改 </button> ' +
                    ' <button id="btn_edit" type="button" class="btn btn-danger btn-xs" onclick="deletes(\'' + value + '\')"><i class="fa fa-trash-o"></i> 删除 </button> ';
            }
        }
    ];
    $("#table").bootstrapTreeTable({
        code: 'id',
        parentCode: 'parentId',
        type: 'get',
        url: '/admin/sysMenu/info',
        striped: false,
        expandColumn: '0',
        bordered: true,
        columns: columns,
        toolbar: toolbar1,
    });
}

//初始化ztree树
function createZtree(){
    initTree('/admin/sysMenu/zTreeNodes', 'typeTree', 0, 'parentName', 'parentId');
}

//清空表单
function clearForm() {
    $("#menuId").val("");
    $("#name").val("");
    $("#parentId").val("");
    $("#parentName").val("");
    $("#hrefs").val("");
    $("#permission").val("");
    $("#icon").val("");
    $("#sort").val("");
    $("#menuType").val("");
    $("#show").iCheck('check');
}

function openMenuTree() {
    openTree('zTree');
}

/**
 * 新增
 */
function add() {
    formType = 0;
    clearForm();
    changeForm();
    menuForm = layer.open({
        type: 1,
        area: '60%',
        title: '新增',
        shade: 0.6,
        shadeClose: true, //点击遮罩关闭层
        maxmin: false,
        content: $("#menu_form"),
        success: function (layero, index) {
            layer.iframeAuto(index);
        },
        end:function () {
            $("#menu_form").hide();
        }
    });
}

function save() {
    var obj = {
        id: $("#menuId").val(),
        parentId: $("#parentId").val(),
        menuType: $("#menuType").val(),
        name: $("#name").val(),
        permission: $("#permission").val(),
        href: $("#hrefs").val(),
        icon: $("#icon").val(),
        sort: $("#sort").val(),
        isShows: $("input[name='isShow']:checked").val(),
    };

    if (obj.menuType == null || obj.menuType == "") { layer.msg("菜单类型不能为空！"); return false; }
    if (obj.name == null || obj.name == "") { layer.msg("菜单名称不能为空！"); return false; }
    if (obj.permission == null || obj.permission == "") { layer.msg("权限标识不能为空！"); return false; }
    if (obj.sort == null || obj.sort == "") { layer.msg("菜单排序不能为空！"); return false; }
    if (obj.isShows == null || obj.isShows == "") { layer.msg("菜单状态不能为空！"); return false; }

    request('/admin/sysMenu/save', 1, obj, function (res) {
        if (res.code == 0) {
            layer.close(menuForm);
            layer.msg("保存成功", {time: 1000}, function () {
                location.reload();
            });
        }else {
            layer.msg(res.msg, {time: 1000,icon:5});
        }
    }, function (error) {
        layer.msg("系统异常！");
    });
}

/**
 * 修改
 */
function update(id) {
    formType = 1;
    clearForm();
    var obj = {
        id: id
    }
    request('/admin/sysMenu/getById', '0', obj, function (data) {
        $("#menuId").val(data.id);
        $("#parentId").val(data.parentId);
        $("#parentName").val(data.parentName);
        $("#menuType").val(data.menuType);
        $("#name").val(data.name);
        $("#permission").val(data.permission);
        $("#hrefs").val(data.href);
        $("#icon").val(data.icon);
        $("#sort").val(data.sort);
        if(data.isShows == 0){
            $("input[name='isShow']#show").iCheck('check');
        }else{
            $("input[name='isShow']#hidden").iCheck('check');
        }
        changeForm();
    }, function () {
        layer.msg("系统异常！");
    });
    menuForm = layer.open({
        type: 1,
        area: '60%',
        title: '修改',
        shade: 0.6,
        shadeClose: true, //点击遮罩关闭层
        maxmin: false,
        content: $("#menu_form"),
        success: function (layero, index) {
            layer.iframeAuto(index);
        },
        end:function () {
            $("#menu_form").hide();
        }
    });
}

function deletes(id) {
    var data = {
        id: id
    };

    layer.confirm('如存在下级节点会一并删除，确定删除吗？', {
        title: '提示',
        btn: ['确定', '取消']
    }, function () {
        request('/admin/sysMenu/delete', 1, data, function (res) {
            if (res.code == 0) {
                layer.msg("删除成功", {time: 1000}, function () {
                    location.reload();
                })
            } else {
                layer.msg(res.msg, {time: 1000});
            }
        }, function (error) {
            layer.msg("系统异常！");
        });
    },function () {
        layer.close(layer.index);
    });
}

function changeForm() {
    $("#form .form-group").hide();
    var menuType = $("#menuType").val();
    if(menuType == '0'){//目录
        $("#form .directoryShow").show();
        if(formType == 0){
            $("#hrefs").val('');
            $("#icon").val('fa fa-folder');
        }
    }else if(menuType == '1'){//菜单
        $("#form .menuShow").show();
        if(formType == 0){
            $("#icon").val('fa fa-file-text');
        }
    }else if(menuType == '2'){//按钮
        $("#form .buttonShow").show();
        if(formType == 0){
            $("#hrefs").val('');
            $("#icon").val('');
        }
    }else{
        $("#form .form-group").show();
    }
}
